package cn.lyh.edu.controller;


import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduNews;
import cn.lyh.edu.service.IEduNewsService;
import cn.lyh.edu.service.dto.NewsQueryCriteria;
import cn.lyh.utils.PageVo;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping ("news")
public class EduNewsController {
    private final IEduNewsService eduNewsService;

    public EduNewsController(IEduNewsService eduNewsService) {
        this.eduNewsService = eduNewsService;
    }

//    @EnableSysLog("获取新闻资讯列表数据")
    @GetMapping
    public ResponseEntity<Object> getList(NewsQueryCriteria queryCriteria,
                                          PageVo pageVo){
        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id");
        return new ResponseEntity<>(eduNewsService.getList(queryCriteria,pageable), HttpStatus.OK);
    }

//    @EnableSysLog("新增新闻资讯信息")
    @PostMapping
    public BaseResult addNews(@RequestBody EduNews eduNews){
        eduNewsService.addNews(eduNews);
        return BaseResult.success("添加成功");
    }

//    @EnableSysLog("根据ID获取新闻资讯的详情信息")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("获取信息失败");
        }
        return BaseResult.success(eduNewsService.getById(id));
    }

//    @EnableSysLog("更新新闻资讯信息")
    @PutMapping
    public BaseResult editNews(@RequestBody EduNews eduNews){
        eduNewsService.editNews(eduNews);
        return BaseResult.success("更新成功");
    }

//    @EnableSysLog("删除新闻资讯信息")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("删除信息失败");
        }
        eduNewsService.deleteById(id);
        return BaseResult.success("删除成功");
    }



}
